﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Web.UI;
using Antu.WebControls;
using Antu.Core.Components;
using Antu.Core.Services;
using System.Web.UI.WebControls;
using EluZSProvider;
using System.Web;
using System.Web.UI.HtmlControls;
using System.Data;

namespace WebSys.UI.Control.ZhengShou
{
    public class EditZhengShouXukeControl : MPBasicTemplate
    {
        EluZSDataProvider _EluZSDataProvider = (EluZSDataProvider)ProviderService.ProviderServiceMange.GetProvider("EluZSProvider");
        public string userid = string.Empty;
        protected override void OnLoad(EventArgs e)
        {
            if (HttpContext.Current.Session["UserID"] == null)
            {
                Antu.Utilities.UtilitiesPage.AlertDialogClear("登录超时，请重新登录！", "window.parent.location.href('../login.aspx');");
            }
            else
            {
                userid = HttpContext.Current.Session["UserID"].ToString();
            }
            base.OnLoad(e);
            MPPersistence Persistence1 = (MPPersistence)FindControl("Persistence1");
            Panel panel1 = (Panel)FindControl("panel1");
            if (!base.Page.IsPostBack)
            {
                Persistence1.Table = "ZS_PRONO";
                Persistence1.Key = "ID";
                Persistence1.KeyValue = HttpContext.Current.Request["id"];
                Persistence1.ConnStr = _EluZSDataProvider.GetConnectionString();
            }
            Persistence1.BindControl = panel1;
            BindData();
            IsLookPerson();
        }

        private void IsLookPerson()
        {
            string sql = "Select rolename from zs_role where id in (Select roleid from zs_user_in_role where userid = '" + userid + "')";
            DataTable dt = _EluZSDataProvider.GetTable(sql);
            if (dt.Rows.Count > 0)
            {
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    if (dt.Rows[i]["rolename"].ToString().Equals("查看人员"))
                    {
                        Button btnSave = (Button)FindControl("btnSave");
                        btnSave.Visible = false;
                    }
                }
            }
        }


        protected override void OnPreRender(EventArgs e)
        {
            base.OnPreRender(e);
            string pid = HttpContext.Current.Request["pid"];
            EluZSProvider.Project project = _EluZSDataProvider.GetProjectXX(pid);
            Label lbl_申请单位 = (Label)FindControl("lbl_申请单位");
            Label lbl_实施单位 = (Label)FindControl("lbl_实施单位");
            Label lbl项目名称 = (Label)FindControl("lbl项目名称");
            Label lbl评估机构 = (Label)FindControl("lbl评估机构");
            Label lbl项目地址 = (Label)FindControl("lbl项目地址");
            TextBox txt拆迁范围 = (TextBox)FindControl("txt拆迁范围");
            Label lbl征收部门 = (Label)FindControl("lbl征收部门");
            TextBox txtPID = (TextBox)FindControl("txtPID");
            lbl_申请单位.Text = project.申请单位; 
            lbl_实施单位.Text = project.征收实施单位; 
            lbl项目名称.Text = project.项目名称; 
            lbl评估机构.Text = project.概算评估机构; 
            lbl项目地址.Text = project.项目地址;
            if (string.IsNullOrEmpty(txt拆迁范围.Text.Trim())) { txt拆迁范围.Text = project.征收范围; }
            lbl征收部门.Text = project.征收部门;
            if (string.IsNullOrEmpty(txtPID.Text.Trim())) { txtPID.Text = project.Id; }
            EluZSProvider.BCFAQS bcfaqs = _EluZSDataProvider.GetBCFAQS("1", pid);
            TextBox txt拆迁居民权证户数 = (TextBox)FindControl("txt拆迁居民权证户数");
            TextBox txt拆迁工企单位个数 = (TextBox)FindControl("txt拆迁工企单位个数");
            TextBox txt拆迁期限 = (TextBox)FindControl("txt拆迁期限");
            if (string.IsNullOrEmpty(txt拆迁居民权证户数.Text.Trim())) { txt拆迁居民权证户数.Text = bcfaqs.权证户数; }
            if (string.IsNullOrEmpty(txt拆迁工企单位个数.Text.Trim())) { txt拆迁工企单位个数.Text = bcfaqs.工企单位; }
            if (string.IsNullOrEmpty(txt拆迁期限.Text.Trim())) { txt拆迁期限.Text = bcfaqs.征收签约期限; }
            TextBox txt征收许可证号 = (TextBox)FindControl("txt征收许可证号");
            HtmlInputText Text1 = (HtmlInputText)FindControl("Text1");
            HtmlInputText Text2 = (HtmlInputText)FindControl("Text2");
            HtmlInputText Text3 = (HtmlInputText)FindControl("Text3");
            if (!string.IsNullOrEmpty(txt征收许可证号.Text.Trim())) {
                string myzs = txt征收许可证号.Text;
                string tt1 = myzs.Replace("宁", "|");
                string tt2 = tt1.Replace("府征字（", "|");
                string tt3 = tt2.Replace("年第", "|");
                string tt4 = tt3.Replace("号）", "|");
                string tt5 = tt4.Replace("|", ",");
                String[] tt6 = tt5.Split(',');
                if (tt6.Length > 1)
                {
                    Text1.Value = tt6[1];
                    Text2.Value = tt6[2];
                    Text3.Value = tt6[3];
                }
            }
        }
        protected void BindData() {
            string pid = HttpContext.Current.Request["pid"];
            Button btnUpfile = (Button)FindControl("btnUpfile");
            Button btnSave = (Button)FindControl("btnSave");
            LinkButton LinkButton1 = (LinkButton)FindControl("LinkButton1");

            btnUpfile.Click += new EventHandler(btnUpfile_Click);
            btnSave.Click += new EventHandler(btnSave_Click);
            LinkButton1.Click += new EventHandler(LinkButton1_Click);

            if (!_EluZSDataProvider.IsNetZSAdmin(userid) && !_EluZSDataProvider.IsZSAdmin(userid) && !_EluZSDataProvider.IsSZSB(userid))
            {
                LinkButton1.Visible = false;
            }
        }
        protected void LinkButton1_Click(object sender, EventArgs e)
        {
            HttpContext.Current.Response.Redirect("ZhengSFWLog.aspx?type=4&pid=" + HttpContext.Current.Request["PID"]);
        }
        protected void btnSave_Click(object sender, EventArgs e)
        {
            MPPersistence Persistence1 = (MPPersistence)FindControl("Persistence1");
            Persistence1.Update();
            //HttpContext.Current.Response.Redirect("EditZhengShouXuke.aspx?ID=" + Persistence1.KeyValue + "&pid=" + HttpContext.Current.Request["PID"] + "&action=edit");
            string url = "EditZhengShouXuke.aspx?ID=" + Persistence1.KeyValue + "&pid=" + HttpContext.Current.Request["PID"] + "&action=edit";
            base.Page.ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('保存成功');location.href='" + url + "'</script>");
        }
        protected void btnUpfile_Click(object sender, EventArgs e)
        {
            TextBox 提交状态 = (TextBox)FindControl("txt提交状态");
            MPPersistence Persistence1 = (MPPersistence)FindControl("Persistence1");
            if (!string.IsNullOrEmpty(Persistence1.KeyValue))
            {
                if (提交状态.Text.Trim()=="1")
                {
                    string strScript = string.Format("<script>OpenFileUpload('{0}','{1}','{2}','{3}');</script>",
                   Persistence1.KeyValue, HttpContext.Current.Request["PID"].Trim(), HttpContext.Current.Request["PID"].Trim(),"show");
                    Page.ClientScript.RegisterStartupScript(this.GetType(), "opennew", strScript);
                }
                else
                {
                    string strScript = string.Format("<script>OpenFileUpload('{0}','{1}','{2}','{3}');</script>",
                   Persistence1.KeyValue, HttpContext.Current.Request["PID"].Trim(), HttpContext.Current.Request["PID"].Trim(),"");
                    Page.ClientScript.RegisterStartupScript(this.GetType(), "opennew", strScript);
                }
               
            }
        }
    }
}


